﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinToolbars;
using Infragistics.Win.UltraWinGrid;

namespace Billing.Win
{
    public partial class ucClientsList : Billing.WindowsBase.CustomControls.BaseControl2007
    {
        public event EventHandler CurrentChanged;
        public event EventHandler ChangePage;
        public event ToolClickEventHandler ToolClick;
        public event EventHandler DoubleClick;

        private int pageNumber;

        public int PageNumber
        {
            get { return pageNumber; }
        }

        public object DataSource
        {
            get
            {
                return clientsViewBindingSource.DataSource;
            }
            set
            {
                clientsViewBindingSource.DataSource = value;
                grClients.DataBind();
            }
        }

        public Billing.DataObjects.ClientsView DataObject
        {
            get
            {
                return clientsViewBindingSource.Current as Billing.DataObjects.ClientsView;
            }
        }

        public BindingSource BindingSource
        {
            get
            {
                return this.clientsViewBindingSource;
            }
        }

        private SearchStatistics searchStatistics;

        public SearchStatistics SearchStatistics
        {
            get { return searchStatistics; }
            set
            {
                searchStatistics = value;
                if (value != null)
                    SetStatusAndPager();
            }
        }

        public ucClientsList()
        {
            InitializeComponent();
            this.CaptionVisibled = false;
        }

        private void ucClientsList_Load(object sender, EventArgs e)
        {
            InitializeGrid();
        }

        private void InitializeGrid()
        {
            Infragistics.Win.UltraWinGrid.ColumnsCollection cols = grClients.DisplayLayout.Bands[0].Columns;

            SetColumnProperties(cols, "ID", "ID", false, 45);
            SetColumnProperties(cols, "IdentificationNumber", "ს/კ", false, 65);
            SetColumnProperties(cols, "Name", "სახელი", false, 300);
            SetColumnProperties(cols, "Address", "მისამართი", false, 180);
            SetColumnProperties(cols, "ClientStatus", "სტატუსი", false, 30);
            SetColumnProperties(cols, "ClientStatusID", "ClientStatusID", true, 30);
            SetColumnProperties(cols, "ClientType", "ტიპი", false, 70);
            SetColumnProperties(cols, "ClientTypeID", "ClientTypeID", true, 30);
            SetColumnProperties(cols, "ContactInfo", "საკონ. ინფ.", false, 120);
            SetColumnProperties(cols, "CreatedDate", "შექმნის თარიღი", true, 30);
            SetColumnProperties(cols, "CreatorID", "CreatorID", true, 30);
            SetColumnProperties(cols, "CurrentObject", "CurrentObject", true, 30);
            SetColumnProperties(cols, "Email", "Email", true, 30);
            SetColumnProperties(cols, "FirstName", "FirstName", true, 30);


            SetColumnProperties(cols, "Key", "Key", true, 30);
            SetColumnProperties(cols, "LastName", "LastName", true, 30);
            SetColumnProperties(cols, "Comment", "კომენტარი", true, 30);
            SetColumnProperties(cols, "ModifiedDate", "შეცვლის თარიღი", true, 30);

            SetColumnProperties(cols, "RowVersion", "RowVersion", true, 30);
            SetColumnProperties(cols, "Tax", "დღგ-ს სტატუსი", false, 50);
            SetColumnProperties(cols, "TaxID", "TaxID", true, 30);
            SetColumnProperties(cols, "UserName", "UserName", true, 30);
            SetColumnProperties(cols, "UserFullName", "ჩანაწერი ავტორი", true, 30);
            SetColumnProperties(cols, "Value", "Value", true, 30);
            SetColumnProperties(cols, "Balance", "ბალანსი", true, 30);
            SetColumnProperties(cols, "ExistsInDB", "ExistsInDB", true, 30);
            SetColumnProperties(cols, "ContractNumber", "კონტრ. N", false, 70);
            SetColumnProperties(cols, "ContractDate", "კონტრ. თარიღი", false, 70);

            SetColumnProperties(cols, "Budgeted", "საბიუჯეტო", false, 50);
        }

        private void SetColumnProperties(Infragistics.Win.UltraWinGrid.ColumnsCollection cols, string columnName, string columnCaption, bool hidden, int width)
        {
            //cols[columnName].Hidden = hidden;
            cols[columnName].Header.Caption = columnCaption;
            //cols[columnName].Width = width;

        }

        private void clientsViewBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            OnCurrentChanged(e);
        }

        private void OnCurrentChanged(EventArgs e)
        {
            if (CurrentChanged != null)
                CurrentChanged(this, e);
        }
        private void btPagerGO_Click(object sender, EventArgs e)
        {

        }

        bool initializingPager = false;

        private void SetStatusAndPager()
        {
            initializingPager = true;
            int rowsFound = searchStatistics.Count;
            int rowsPerPage = searchStatistics.PagerSettings.RowsPerPage;
            int currenPage = searchStatistics.PagerSettings.CurrentPageNumber;
            int pageCount = rowsFound / rowsPerPage + 1;

            lbOveralStatus.Text = string.Format("ნაპოვნია {0} ჩანაწერი;", rowsFound);
            valPager.Items.Clear();

            for (int i = 0; i < pageCount; i++)
            {
                int currentRowsCount = rowsPerPage;
                if (((i + 1) * rowsPerPage) > rowsFound)
                {
                    currentRowsCount = ((i * rowsPerPage) <= 0) ? rowsFound : rowsFound % (i * rowsPerPage);
                }
                valPager.Items.Add(new PagerListViewItem(i + 1, pageCount, currentRowsCount, rowsFound));
            }

            valPager.SelectedIndex = currenPage - 1;
            initializingPager = false;
        }

        private void OnChangePage(EventArgs e)
        {
            if (initializingPager) return;

            PagerListViewItem value = valPager.Value as PagerListViewItem;
            if (value == null) return;

            this.pageNumber = value.Value;

            if (ChangePage != null)
            {
                ChangePage(this, e);
            }
        }

        private void valPager_SelectionChanged(object sender, EventArgs e)
        {
            if (!initializingPager)
                OnChangePage(e);
        }

        private void grClients_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            Billing.DataObjects.ClientsView cv = e.Row.ListObject as Billing.DataObjects.ClientsView;
            if (cv == null) return;
            switch (cv.ClientStatusID)
            {
                case 1:
                    e.Row.Cells["STIMG"].Value = Billing.Win.Properties.Resources.yellow;
                    break;
                case 2:
                    e.Row.Cells["STIMG"].Value = Billing.Win.Properties.Resources.green;
                    break;
                case 3:
                    e.Row.Cells["STIMG"].Value = Billing.Win.Properties.Resources.red;
                    break;
            }

        }

        private void btPrevClick(object sender, EventArgs e)
        {
            valPager.SelectedIndex -= 1;
        }

        private void btNextClick(object sender, EventArgs e)
        {
            valPager.SelectedIndex += 1;
        }

        private void TBM_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            if (ToolClick != null)
                ToolClick(TBM, e);
        }

        private void grClients_Click(object sender, EventArgs e)
        {
            if (clientsViewBindingSource.Count == 0)
                if (CurrentChanged != null)
                    CurrentChanged(this, e);
        }

        #region Grid Row DoubleClick Event
        UltraGridRow PupUpRow;
        System.Collections.BitArray GridEvents = new System.Collections.BitArray(2, false);

        private enum GridElements
        {
            Row,
            GroupBox
        }

        private void Grid_MouseEnterElement(object sender, Infragistics.Win.UIElementEventArgs e)
        {
            try
            {
                if (e.Element is CellUIElement || e.Element is RowSelectorUIElement)
                {
                    this.SetGridEventValue(GridElements.Row, true);
                    if (e.Element is CellUIElement)
                        PupUpRow = ((CellUIElement)e.Element).Row;
                    else if (e.Element is RowSelectorUIElement)
                        PupUpRow = ((RowSelectorUIElement)e.Element).Row;
                }
                else if (e.Element is GroupByBoxUIElement)
                    this.SetGridEventValue(GridElements.GroupBox, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void Grid_MouseLeaveElement(object sender, Infragistics.Win.UIElementEventArgs e)
        {
            try
            {
                if (e.Element is CellUIElement || e.Element is RowSelectorUIElement)
                {
                    this.SetGridEventValue(GridElements.Row, false);
                    PupUpRow = null;
                }
                else if (e.Element is GroupByBoxUIElement)
                    this.SetGridEventValue(GridElements.GroupBox, false);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.StackTrace); }

        }

        private void Grid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                switch (e.Button)
                {
                    case MouseButtons.Left:
                        if (e.Clicks == 2 && this.GetGridEventValue(GridElements.Row))
                        {
                            if (this.DoubleClick != null)
                                this.DoubleClick(this, EventArgs.Empty);
                        }
                        else if (e.Clicks == 2 && this.GetGridEventValue(GridElements.GroupBox))
                            this.grClients.ShowColumnChooser(true);
                        break;
                    case MouseButtons.Middle:
                        break;
                    case MouseButtons.None:
                        break;
                    case MouseButtons.Right:
                        if (e.Clicks == 1 && this.GetGridEventValue(GridElements.Row))
                            if (PupUpRow != null)
                            {
                                PupUpRow.Activate();
                                PupUpRow.Selected = true;
                            }
                        break;
                    case MouseButtons.XButton1:
                        break;
                    case MouseButtons.XButton2:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void SetGridEventValue(GridElements GridEvent, bool Value)
        { this.GridEvents[(int)GridEvent] = Value; }

        private bool GetGridEventValue(GridElements Gridevent)
        { return this.GridEvents[(int)Gridevent]; }

        #endregion
    }
}
